package com.byxy.ssm.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.ssm.mapper.ReplyMapper;
import com.byxy.ssm.mapper.UserMapper;
import com.byxy.ssm.pojo.Reply;
import com.byxy.ssm.pojo.User;
import com.byxy.ssm.service.ReplyService;
import com.byxy.ssm.vo.ReplyVO;
@Service
@Transactional
public class ReplyServiceImpl implements ReplyService{

	@Resource 
	private ReplyMapper replyMapper;
	@Resource
	private UserMapper userMapper;
	@Override
	public int delete(Integer replyId) {
		// TODO Auto-generated method stub
		return replyMapper.delete(replyId);
	}

	@Override
	public int insert(Integer tipId,String replyContent,Integer userId) {
		// TODO Auto-generated method stub
		Date date=new Date();
		Reply reply=new Reply();
		reply.setUserId(userId);
		reply.setTipId(tipId);
		reply.setReplyContent(replyContent);
		reply.setReplyModifytime(date);
		reply.setReplyPublishtime(date);
		reply.setReplyStatus(0);
		return replyMapper.insert(reply);
	}


	@Override
	public int update(Reply reply) {
		// TODO Auto-generated method stub
		return replyMapper.update(reply);
	}

	@Override
	public List<ReplyVO> getByTipId(Integer tipId) {
		// TODO Auto-generated method stub
		List<Reply> reply=replyMapper.getByTipId(tipId);
		List<ReplyVO> replyVOList=new ArrayList<ReplyVO>();
		for (Reply reVO : reply) {
			ReplyVO replyVO=new ReplyVO();
			BeanUtils.copyProperties(reVO, replyVO);
			User user=userMapper.getById(reVO.getUserId());
			if (user.getUserNick()!=null&&!user.getUserNick().equals("")) {
				replyVO.setUserName(user.getUserNick());
			}else {
				replyVO.setUserName(user.getUserName());
			}
			replyVOList.add(replyVO);
		}
		return replyVOList;
	}
	
	/*@Override
	public Reply getById(Integer replyId) {
		// TODO Auto-generated method stub
		return replyMapper.getById(replyId);
	}

	@Override
	public List<ReplyVO> findAll() {
		// TODO Auto-generated method stub
		
		List<Reply> reply=replyMapper.findAll();
		List<ReplyVO> replyVOList=new ArrayList<ReplyVO>();
		for (Reply reVO : reply) {
			ReplyVO replyVO=new ReplyVO();
			BeanUtils.copyProperties(reVO, replyVO);
			User user=userMapper.getById(reVO.getUserId());
			if (user.getUserNick()!=null&&!user.getUserNick().equals("")) {
				replyVO.setUserName(user.getUserNick());
			}else {
				replyVO.setUserName(user.getUserName());
			}
			replyVOList.add(replyVO);
		}
		return replyVOList;
	}*/

}
